// package_feed/pages/feedback/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl:'/images/icons/img.png',
    barHeight:40,
    isButtonDisabled: true, // 初始禁用按钮
  },
  goBack(){
    wx.navigateBack()
  },
  handleUpload(){
    wx.chooseMedia({
      success:(media)=>{
        console.log('选取本地图像资源',media);
        let tempUrl=media.tempFiles[0].tempFilePath
        // this.setData({imgUrl:tempUrl})
        let str=Math.random().toString(36).substring(2)
        let time=Date.now()
        let cloudPath=str+time+'.png'
        wx.cloud.uploadFile({
          filePath:tempUrl,
          cloudPath,
          success:(res)=>{
            console.log('云存储后下发的路径',res);
            this.setData({
              imgUrl:res.fileID,
              value:res.fileID
            })
            this.triggerEvent('uploaded',res.fileID)
          }
        })
      }
    })
  },
  handleChange(){
    this.setData({
      isButtonDisabled:false
    })
  },
async  submitForm(e){
    console.log('获取反馈信息',e);
    let res=e.detail.value;
    console.log('获取表单信息',res);
    console.log('链接',this.data.imgUrl);
    const feedbackTypes= [
      "消息不及时", 
      "系统卡顿", 
      "无法上传照片"
    ];
    let type=[]
    if(res.a){
      type.push(feedbackTypes[0]); 
    }
    if(res.b){
      type.push(feedbackTypes[1]); 
    }
    if(res.c){
      type.push(feedbackTypes[2]); 
    }
    console.log('type',type);
    let time=Date.now()
    const date = new Date(time);
    console.log('时间',time);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    console.log(formattedTime);
    await getApp().db.collection('feedback').add({
      data:{
        details:res.details,
        photos:this.data.imgUrl,
        type,
        time:formattedTime,
        user:"李伟",
        userId:"eceaf6fa68873eb5004894f50343329a"
      }
    })
    wx.navigateBack()
  },
  onChange(event) {
    this.setData({
      result: event.detail,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let info=wx.getWindowInfo()
    console.log(info);
    this.setData({
      barHeight:info.statusBarHeight*2-20
    })
  },
})